package com.qys.training.biz.sysconfig.service.impl;


import com.qys.training.base.enumerate.BizCodeEnum;
import com.qys.training.base.exception.QysException;
import com.qys.training.base.util.CacheUtils;
import com.qys.training.biz.sysconfig.entity.SysConfig;
import com.qys.training.biz.sysconfig.mapper.SysConfigMapper;
import com.qys.training.biz.sysconfig.service.SysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private CacheUtils cacheUtils;
    public static final Logger logger = LoggerFactory.getLogger(SysConfigServiceImpl.class);
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public Long create(SysConfig config) {
        sysConfigMapper.insert(config);
        return config.getId();
    }

    @Override
    public void delete(String key) {
        Object o = cacheUtils.get("sysconfig",key);
        if( o != null) {
            cacheUtils.del("sysconfig",key);
        }
        sysConfigMapper.delete(key);
    }


    @Override
    public SysConfig getByKey(String key) {
        Object o = cacheUtils.get("sysconfig",key);
        if (o != null) {
            logger.info("缓存取值");
            return (SysConfig) o;
        }
        SysConfig config = sysConfigMapper.get(key);
        if (config == null) {
            throw new QysException(BizCodeEnum.ERROR.getCode(),"不存在该值");
        }
        cacheUtils.add("sysconfig",key,config);
        return config;
    }

}
